package com.cennavi.bigdata.baotou.scripts;

import com.alibaba.fastjson.JSONObject;
import com.mongodb.*;
import org.slf4j.LoggerFactory;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Created by cennavi on 2017/11/27.
 */
public class InsertMongoKakouFlow extends ActualTimeRequest{

    private static final org.slf4j.Logger LOGGER = LoggerFactory.getLogger(InsertMongoKakouFlow.class);

    private DBCollection corresKakouFlow = null;
    private DBCollection corresKakouFlowHour = null;
    private DBCollection corresKakouFlowMinuteWork = null;
    private DBCollection corresKakouFlowMinuteNWork = null;
    private DBCollection corresKakouFlowMinuteHoli = null;

    public void  insertKakouFlowByHour()  {
        String format1 = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss").format(new Date());
        LOGGER.info("insertKakouFlowByHour开始时间：" + format1);
        corresKakouFlow = getMongoCollection("kakouFlow");
        corresKakouFlowHour = getMongoCollection("kakouFlowHour");
        corresKakouFlow.drop();
        BasicDBList dbList = new BasicDBList();
        Calendar calendar = Calendar.getInstance();
        for (int i = 0; i < 3; i++) {
            calendar.add(Calendar.MONTH,-1);
            String month = new SimpleDateFormat("yyyyMM").format(calendar.getTime());
            dbList.add(month);
        }
        DBObject basicDBObject = new BasicDBObject("$in",dbList);
        BasicDBObject queryDBObject = new BasicDBObject();
        queryDBObject.put("date",basicDBObject);

        Set<String> kkbhSet = new HashSet<>();
        Set<String> timeSet = new HashSet<>();
        Set<String> dateTypeSet = new HashSet<>();
        List<DBObject> mongoList = new ArrayList<>();
        DBCursor cursor = corresKakouFlowHour.find(queryDBObject);
        JSONObject jsonObject = new JSONObject();
        while (cursor.hasNext()) {
            DBObject rticObj = cursor.next();
            String kkbh = (String) rticObj.get("kkbh");
            String time = (String) rticObj.get("time");
            String dateType = (String) rticObj.get("dateType");
            int maxKkll = (int) rticObj.get("maxKkll");
            int avgKkll = (int) rticObj.get("avgKkll");
            if (jsonObject.containsKey(kkbh + time +dateType)) {
                JSONObject kakouJson = jsonObject.getJSONObject(kkbh + time +dateType);
                int realAvgKkll = kakouJson.getInteger("avgKkll");
                int realMaxKkll = kakouJson.getInteger("maxKkll");
                realAvgKkll = (realAvgKkll + avgKkll) / 2;
                kakouJson.put("avgKkll", realAvgKkll);
                if (realMaxKkll > maxKkll) {
                    kakouJson.put("maxKkll", realMaxKkll);
                } else {
                    kakouJson.put("maxKkll", maxKkll);
                }
                jsonObject.put(kkbh + time +dateType, kakouJson);
            } else {
                JSONObject kakouJson = new JSONObject();
                kakouJson.put("maxKkll", maxKkll);
                kakouJson.put("avgKkll", avgKkll);
                jsonObject.put(kkbh + time +dateType, kakouJson);
            }
            kkbhSet.add(kkbh);
            timeSet.add(time);
            dateTypeSet.add(dateType);
        }

        Iterator<String> iterator2 = dateTypeSet.iterator();
        while (iterator2.hasNext()){
            String dateType = iterator2.next();
            Iterator<String> iterator = timeSet.iterator();
            while (iterator.hasNext()){
                String time = iterator.next();
                Iterator<String> iterator1 = kkbhSet.iterator();
                while (iterator1.hasNext()){
                    String kkbh = iterator1.next();
                    if (jsonObject.containsKey(kkbh+time+dateType)){
                        JSONObject object = jsonObject.getJSONObject(kkbh + time+dateType);
                        int maxKkll = object.getInteger("maxKkll");
                        int avgKkll = object.getInteger("avgKkll");
                        DBObject json = new BasicDBObject();
                        json.put("kkbh",kkbh);
                        json.put("time",time);
                        json.put("maxKkll",maxKkll);
                        json.put("avgKkll",avgKkll);
                        json.put("dateType",dateType);
                        json.put("timeType","Hour");
                        mongoList.add(json);
                    }
                }
            }
        }

        if (mongoList.size() > 0) {
            corresKakouFlow.insert(mongoList);
        }
        corresKakouFlow.createIndex("kkbh");
        corresKakouFlow.createIndex("time");
        corresKakouFlow.createIndex("maxKkll");
        corresKakouFlow.createIndex("avgKkll");
        corresKakouFlow.createIndex("dateType");
        corresKakouFlow.createIndex("timeType");
        String format = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss").format(new Date());
        LOGGER.info("insertKakouFlowByHour完成时间：" + format);
        insertKakouFlowByMinuteWork();
        insertKakouFlowByMinuteNWork();
        insertKakouFlowByMinuteHoli();
    }

    //工作日数据插入
    public void  insertKakouFlowByMinuteWork(){
        String format1 = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss").format(new Date());
        LOGGER.info("insertKakouFlowByMinuteWork开始时间：" + format1);
        corresKakouFlow = getMongoCollection("kakouFlow");
        corresKakouFlowMinuteWork = getMongoCollection("kakouFlowMinuteWork");

        BasicDBList dbList = new BasicDBList();
        Calendar calendar = Calendar.getInstance();
        for (int i = 0; i < 3; i++) {
            calendar.add(Calendar.MONTH,-1);
            String month = new SimpleDateFormat("yyyyMM").format(calendar.getTime());
            dbList.add(month);
        }
        DBObject basicDBObject = new BasicDBObject("$in",dbList);
        BasicDBObject queryDBObject = new BasicDBObject();
        queryDBObject.put("date",basicDBObject);

        Set<String> kkbhSet = new HashSet<>();
        Set<String> timeSet = new HashSet<>();
        List<DBObject> mongoList = new ArrayList<>();
        DBCursor cursor = corresKakouFlowMinuteWork.find(queryDBObject);
        JSONObject jsonObject = new JSONObject();
        while (cursor.hasNext()) {
            DBObject rticObj = cursor.next();
            String kkbh = (String) rticObj.get("kkbh");
            String time = (String) rticObj.get("time");
            String dateType = (String) rticObj.get("dateType");
            int maxKkll = (int) rticObj.get("maxKkll");
            int avgKkll = (int) rticObj.get("avgKkll");
            if (jsonObject.containsKey(kkbh + time)) {
                JSONObject kakouJson = jsonObject.getJSONObject(kkbh + time);
                int realAvgKkll = kakouJson.getInteger("avgKkll");
                int realMaxKkll = kakouJson.getInteger("maxKkll");
                realAvgKkll = (realAvgKkll + avgKkll) / 2;
                kakouJson.put("avgKkll", realAvgKkll);
                if (realMaxKkll > maxKkll) {
                    kakouJson.put("maxKkll", realMaxKkll);
                } else {
                    kakouJson.put("maxKkll", maxKkll);
                }
                jsonObject.put(kkbh + time, kakouJson);
            } else {
                JSONObject kakouJson = new JSONObject();
                kakouJson.put("dateType", dateType);
                kakouJson.put("maxKkll", maxKkll);
                kakouJson.put("avgKkll", avgKkll);
                jsonObject.put(kkbh + time, kakouJson);
            }
            kkbhSet.add(kkbh);
            timeSet.add(time);
        }

        Iterator<String> iterator = timeSet.iterator();
        while (iterator.hasNext()){
            String next = iterator.next();
            Iterator<String> iterator1 = kkbhSet.iterator();
            while (iterator1.hasNext()){
                String next1 = iterator1.next();
                if (jsonObject.containsKey(next1+next)){
                    JSONObject object = jsonObject.getJSONObject(next1 + next);
                    String dateType = object.getString("dateType");
                    int maxKkll = object.getInteger("maxKkll");
                    int avgKkll = object.getInteger("avgKkll");
                    DBObject json = new BasicDBObject();
                    json.put("kkbh",next1);
                    json.put("time",next);
                    json.put("maxKkll",maxKkll);
                    json.put("avgKkll",avgKkll);
                    json.put("dateType",dateType);
                    json.put("timeType","Minute");
                    mongoList.add(json);
                }
            }
        }
        if (mongoList.size() > 0) {
            corresKakouFlow.insert(mongoList);
        }
        corresKakouFlow.createIndex("kkbh");
        corresKakouFlow.createIndex("time");
        corresKakouFlow.createIndex("maxKkll");
        corresKakouFlow.createIndex("avgKkll");
        corresKakouFlow.createIndex("dateType");
        corresKakouFlow.createIndex("timeType");
        String format = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss").format(new Date());
        LOGGER.info("insertKakouFlowByMinuteWork完成时间：" + format);
    }

    //非工作日数据插入
    public void  insertKakouFlowByMinuteNWork(){
        String format1 = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss").format(new Date());
        LOGGER.info("insertKakouFlowByMinuteNWork开始时间：" + format1);
        corresKakouFlow = getMongoCollection("kakouFlow");
        corresKakouFlowMinuteNWork = getMongoCollection("kakouFlowMinuteNWork");

        BasicDBList dbList = new BasicDBList();
        Calendar calendar = Calendar.getInstance();
        for (int i = 0; i < 3; i++) {
            calendar.add(Calendar.MONTH,-1);
            String month = new SimpleDateFormat("yyyyMM").format(calendar.getTime());
            dbList.add(month);
        }
        DBObject basicDBObject = new BasicDBObject("$in",dbList);
        BasicDBObject queryDBObject = new BasicDBObject();
        queryDBObject.put("date",basicDBObject);

        Set<String> kkbhSet = new HashSet<>();
        Set<String> timeSet = new HashSet<>();
        List<DBObject> mongoList = new ArrayList<>();
        DBCursor cursor = corresKakouFlowMinuteNWork.find(queryDBObject);
        JSONObject jsonObject = new JSONObject();
        while (cursor.hasNext()) {
            DBObject rticObj = cursor.next();
            String kkbh = (String) rticObj.get("kkbh");
            String time = (String) rticObj.get("time");
            String dateType = (String) rticObj.get("dateType");
            int maxKkll = (int) rticObj.get("maxKkll");
            int avgKkll = (int) rticObj.get("avgKkll");
            if (jsonObject.containsKey(kkbh + time)) {
                JSONObject kakouJson = jsonObject.getJSONObject(kkbh + time);
                int realAvgKkll = kakouJson.getInteger("avgKkll");
                int realMaxKkll = kakouJson.getInteger("maxKkll");
                realAvgKkll = (realAvgKkll + avgKkll) / 2;
                kakouJson.put("avgKkll", realAvgKkll);
                if (realMaxKkll > maxKkll) {
                    kakouJson.put("maxKkll", realMaxKkll);
                } else {
                    kakouJson.put("maxKkll", maxKkll);
                }
                jsonObject.put(kkbh + time, kakouJson);
            } else {
                JSONObject kakouJson = new JSONObject();
                kakouJson.put("dateType", dateType);
                kakouJson.put("maxKkll", maxKkll);
                kakouJson.put("avgKkll", avgKkll);
                jsonObject.put(kkbh + time, kakouJson);
            }
            kkbhSet.add(kkbh);
            timeSet.add(time);
        }

        Iterator<String> iterator = timeSet.iterator();
        while (iterator.hasNext()){
            String next = iterator.next();
            Iterator<String> iterator1 = kkbhSet.iterator();
            while (iterator1.hasNext()){
                String next1 = iterator1.next();
                if (jsonObject.containsKey(next1+next)){
                    JSONObject object = jsonObject.getJSONObject(next1 + next);
                    String dateType = object.getString("dateType");
                    int maxKkll = object.getInteger("maxKkll");
                    int avgKkll = object.getInteger("avgKkll");
                    DBObject json = new BasicDBObject();
                    json.put("kkbh",next1);
                    json.put("time",next);
                    json.put("maxKkll",maxKkll);
                    json.put("avgKkll",avgKkll);
                    json.put("dateType",dateType);
                    json.put("timeType","Minute");
                    mongoList.add(json);
                }
            }
        }
        if (mongoList.size() > 0) {
            corresKakouFlow.insert(mongoList);
        }
        corresKakouFlow.createIndex("kkbh");
        corresKakouFlow.createIndex("time");
        corresKakouFlow.createIndex("maxKkll");
        corresKakouFlow.createIndex("avgKkll");
        corresKakouFlow.createIndex("dateType");
        corresKakouFlow.createIndex("timeType");
        String format = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss").format(new Date());
        LOGGER.info("insertKakouFlowByMinuteNWork完成时间：" + format);
    }

    //节假日数据插入
    public void  insertKakouFlowByMinuteHoli(){
        String format3 = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss").format(new Date());
        LOGGER.info("insertKakouFlowByMinuteHoli完成时间：" + format3);
        corresKakouFlow = getMongoCollection("kakouFlow");
        corresKakouFlowMinuteHoli = getMongoCollection("kakouFlowMinuteHoli");

        BasicDBList dbList = new BasicDBList();
        Calendar calendar = Calendar.getInstance();
        for (int i = 0; i < 3; i++) {
            calendar.add(Calendar.MONTH,-1);
            String month = new SimpleDateFormat("yyyyMM").format(calendar.getTime());
            dbList.add(month);
        }
        DBObject basicDBObject = new BasicDBObject("$in",dbList);
        BasicDBObject queryDBObject = new BasicDBObject();
        queryDBObject.put("date",basicDBObject);

        Set<String> kkbhSet = new HashSet<>();
        Set<String> timeSet = new HashSet<>();
        List<DBObject> mongoList = new ArrayList<>();
        DBCursor cursor = corresKakouFlowMinuteHoli.find(queryDBObject);
        JSONObject jsonObject = new JSONObject();
        while (cursor.hasNext()) {
            DBObject rticObj = cursor.next();
            String kkbh = (String) rticObj.get("kkbh");
            String time = (String) rticObj.get("time");
            String dateType = (String) rticObj.get("dateType");
            int maxKkll = (int) rticObj.get("maxKkll");
            int avgKkll = (int) rticObj.get("avgKkll");
            if (jsonObject.containsKey(kkbh + time)) {
                JSONObject kakouJson = jsonObject.getJSONObject(kkbh + time);
                int realAvgKkll = kakouJson.getInteger("avgKkll");
                int realMaxKkll = kakouJson.getInteger("maxKkll");
                realAvgKkll = (realAvgKkll + avgKkll) / 2;
                kakouJson.put("avgKkll", realAvgKkll);
                if (realMaxKkll > maxKkll) {
                    kakouJson.put("maxKkll", realMaxKkll);
                } else {
                    kakouJson.put("maxKkll", maxKkll);
                }
                jsonObject.put(kkbh + time, kakouJson);
            } else {
                JSONObject kakouJson = new JSONObject();
                kakouJson.put("dateType", dateType);
                kakouJson.put("maxKkll", maxKkll);
                kakouJson.put("avgKkll", avgKkll);
                jsonObject.put(kkbh + time, kakouJson);
            }
            kkbhSet.add(kkbh);
            timeSet.add(time);
        }

        Iterator<String> iterator = timeSet.iterator();
        while (iterator.hasNext()){
            String next = iterator.next();
            Iterator<String> iterator1 = kkbhSet.iterator();
            while (iterator1.hasNext()){
                String next1 = iterator1.next();
                if (jsonObject.containsKey(next1+next)){
                    JSONObject object = jsonObject.getJSONObject(next1 + next);
                    String dateType = object.getString("dateType");
                    int maxKkll = object.getInteger("maxKkll");
                    int avgKkll = object.getInteger("avgKkll");
                    DBObject json = new BasicDBObject();
                    json.put("kkbh",next1);
                    json.put("time",next);
                    json.put("maxKkll",maxKkll);
                    json.put("avgKkll",avgKkll);
                    json.put("dateType",dateType);
                    json.put("timeType","Minute");
                    mongoList.add(json);
                }
            }
        }
        if (mongoList.size() > 0) {
            corresKakouFlow.insert(mongoList);
        }
        corresKakouFlow.createIndex("kkbh");
        corresKakouFlow.createIndex("time");
        corresKakouFlow.createIndex("maxKkll");
        corresKakouFlow.createIndex("avgKkll");
        corresKakouFlow.createIndex("dateType");
        corresKakouFlow.createIndex("timeType");
        String format = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss").format(new Date());
        LOGGER.info("insertKakouFlowByMinuteHoli完成时间：" + format);
        closeMongo();
    }
}
